import numpy as np
import json
from StringIO import StringIO

def array_2_string(array):
  str_out = StringIO()
  np.savetxt(str_out, array)
  return str_out.getvalue()

def string_2_array(string):
  str_in = StringIO(string)
  return np.loadtxt(str_in)

def main():
  hidden_units=1024
  feature_dim=451
  ivec_dim=25
  nnet_dict = {}
  base_dnn='exp_pdnn/dnn5_fbank/nnet.finetune.tmp'
  ptr_file='exp_pdnn/warm5_ivec_dnn_fbank/dnn.ptr'
  base_dnn_file = open(base_dnn,'r')
  nnet_dict = json.load(base_dnn_file)

  #change the input layer weights
  key='0 sigmoid W'
  W=string_2_array(nnet_dict[key])
  rng = np.random.RandomState(89677)
  W1_values = np.asarray(rng.uniform(low=-np.sqrt(6. / (feature_dim + hidden_units)),high=np.sqrt(6. / (feature_dim + hidden_units)),size=(ivec_dim, hidden_units)))
  new_W = np.vstack((W,W1_values))
  nnet_dict[key]=array_2_string(new_W) 

  with open(ptr_file, 'wb') as fp:
    json.dump(nnet_dict, fp, indent=2, sort_keys = True)
    fp.flush()
 
if __name__=="__main__":
    main()

